/* Copyright (c) 2013, Shawn McGregor and Steve Mitchell All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. AS3935.c Notes: */ #include "AS3935.h" #include #include /****************************************************************** Function: char AS3935i2cRead (char Register) Input: Output: Overview: send reset 0x3c value 0x96 ********************************************************************/ char AS3935i2cRead(char address) { unsigned char data = 0; IdleI2C(); StartI2C(); while ( SSPCON2bits.SEN ); // wait until start condition is over WriteI2C( AS3935_Write ); IdleI2C(); WriteI2C( address ); // select register address IdleI2C(); RestartI2C(); while(SSPCON2bits.RSEN); // wait till start condition is over WriteI2C( AS3935_Read ); IdleI2C(); data = ReadI2C(); // get data NotAckI2C(); while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over StopI2C(); while ( SSPCON2bits.PEN ); // wait until stop condition is over return (data); } /****************************************************************** Function: char AS3935i2cWrite(char Register, char Value) Input: Output: Overview: send reset 0x3c value 0x96 ********************************************************************/ char AS3935i2cWrite(char address, char data) { IdleI2C(); StartI2C(); while(SSPCON2bits.SEN); // wait till start condition is over WriteI2C( AS3935_Write ); IdleI2C(); WriteI2C( address ); IdleI2C(); WriteI2C( data ); IdleI2C(); StopI2C(); while ( SSPCON2bits.PEN ); // wait until stop condition is over return (0); } /****************************************************************** Function: char Reset (void) Input: Output: Overview: send reset 0x3c value 0x96 ********************************************************************/ void AS3935Reset(void) { AS3935i2cWrite(0x3C, 0x96); Delay100TCYx (200); // Wait 2 milliseconds } /****************************************************************** Function: char Calibrate (void) Input: Output: Overview: set calibration value ********************************************************************/ char Calibrate (char tunevalue) { AS3935i2cWrite(TUN_CAP, tunevalue); Delay100TCYx (200); // Wait 2 milliseconds AS3935i2cWrite(0x3D, 0x96); // I2C send (0x3D value 0x96) RCO calibration PowerDownEnable(); // Send power off Delay100TCYx (200); // Wait 2 milliseconds PowerDownDisable(); // Send power on Delay100TCYx (200); // Wait 2 milliseconds return(0); } /****************************************************************** Function: char PowerDownEnable (void) Input: Setting = 1 PowerDown Enabled Setting = 0 PowerDown Disabled Output: Overview: ********************************************************************/ char PowerDownEnable(void) { char value = 0; value = AS3935i2cRead(PWD); value = (value & ~MSK_PWD) | (1 & MSK_PWD); AS3935i2cWrite(PWD, value); return (0); } /****************************************************************** Function: char PowerDownDisable (void) Input: Setting = 1 PowerDown Enabled Setting = 0 PowerDown Disabled Output: Overview: ********************************************************************/ char PowerDownDisable(void) { char value = 0; value = AS3935i2cRead(PWD); value = (value & ~MSK_PWD) | (0 & MSK_PWD); AS3935i2cWrite(PWD, value); return (0); } /****************************************************************** Function: char DisturberEnable (void) Input: setting = Enabled 0x20 setting = Disabled 0x00 Output: Overview: ********************************************************************/ char DisturberEnable(void) { char value = 0; value = AS3935i2cRead(DISTURBER); value = (value & ~MSK_DISTURBER) | (0x20 & MSK_DISTURBER); AS3935i2cWrite(DISTURBER, value); return (0); } /****************************************************************** Function: char DisturberDisable (void) Input: setting = Enabled 0x10 setting = Disabled 0x00 Output: Overview: ********************************************************************/ char DisturberDisable(void) { char value = 0; value = AS3935i2cRead(DISTURBER); value = (value & ~MSK_DISTURBER) | (0x00 & MSK_DISTURBER); AS3935i2cWrite(DISTURBER, value); return (0); } /****************************************************************** Function: char MinLightningGet () Input: Output: Overview: ********************************************************************/ char LightningMinGet(void) { char value = 0; value = AS3935i2cRead(MIN_LIGHTNING); value = value & MSK_MIN_LIGHTNING; return (value); } /****************************************************************** Function: char MinLightningSet () Input: Output: Overview: ********************************************************************/ char LightningMinSet(char setting) { char value = 0; value = LightningMinGet(); value = (value & ~MSK_MIN_LIGHTNING) | (setting & MSK_MIN_LIGHTNING); AS3935i2cWrite(MIN_LIGHTNING, value); return (0); } /****************************************************************** Function: char LightningDistKm () Input: Output: Overview: ********************************************************************/ char LightningDistGetKm(void) { char value = 0; value = AS3935i2cRead(DISTANCE); value = value & MSK_DISTANCE; return (value); } /****************************************************************** Function: char UnitLocationSet () Input: Indoor - 10010 Outdoor - 01110 Output: Overview: ********************************************************************/ char UnitLocationSet(char setting) { char value = 0; value = (value & ~MSK_AFE_GB) | (setting & MSK_AFE_GB); AS3935i2cWrite(AFE_GB, value); return (0); } /****************************************************************** Function: char NoiseFloorGet () Input: Output: Overview: ********************************************************************/ char NoiseFloorGet(void) { char value = 0; value = AS3935i2cRead(NF_LEV); value = value & MSK_NF_LEV; return (value); } /****************************************************************** Function: char NoiseFloorSet (char setting) Input: Output: Overview: ********************************************************************/ char NoiseFloorSet(char setting) { char value = 0; value = NoiseFloorGet(); value = (value & ~MSK_NF_LEV) | (setting & MSK_NF_LEV); AS3935i2cWrite(NF_LEV, value); return (0); } /****************************************************************** Function: char SpikeRejectionGet () Input: Output: Overview: ********************************************************************/ char SpikeRejectionGet(void) { char value = 0; value = AS3935i2cRead(SREJ); value = value & MSK_SREJ; return (value); } /****************************************************************** Function: char SpikeRejectionSet () Input: Output: Overview: ********************************************************************/ char SpikeRejectionSet(char setting) { char value = 0; value = SpikeRejectionGet(); value = (value & ~MSK_SREJ) | (setting & MSK_SREJ); AS3935i2cWrite(SREJ, value); return (0); } /****************************************************************** Function: char WatchDogThresholdGet () Input: Output: Overview: ********************************************************************/ char WatchDogThresholdGet(void) { char value = 0; value = AS3935i2cRead(WDTH); value = value & MSK_WDTH; return (value); } /****************************************************************** Function: char WatchDogThresholdSet () Input: Output: Overview: ********************************************************************/ char WatchDogThresholdSet(char setting) { char value = 0; value = WatchDogThresholdGet(); value = (value & ~MSK_WDTH) | (setting & MSK_WDTH); AS3935i2cWrite(WDTH, value); return (0); } /****************************************************************** Function: char ClrStats () Input: Output: Overview: ********************************************************************/ char ClrStats(void) { char value = 0; value = AS3935i2cRead(CL_STAT); value = (value & ~MSK_CL_STAT) | (0x40 & MSK_CL_STAT); AS3935i2cWrite(CL_STAT, value); value = AS3935i2cRead(CL_STAT); value = (value & ~MSK_CL_STAT) | (0x00 & MSK_CL_STAT); AS3935i2cWrite(CL_STAT, value); value = AS3935i2cRead(CL_STAT); value = (value & ~MSK_CL_STAT) | (0x40 & MSK_CL_STAT); AS3935i2cWrite(CL_STAT, value); return (0); }